<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
abstract class AdminhtmlController extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = 'application.modules.admin.views.layouts.column1';
    //public $layout='//../modules/adminhtml/views/layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    public $controllerLable = '';
    public $actionLable = '';

    public function init() {
        parent::init();
    }

    abstract protected function getActionsKey();

    public function getModuleLabel() {
        if ($this->getModule() && $this->getModule()->moduleLabel) {
            return $this->getModule()->moduleLabel;
        } else {
            return Yii::app()->name;
        }
    }

    public function getControllerLabel() {
        if ($this->controllerLable) {
            return $this->controllerLable;
        } else {
            return $this->getId();
        }
    }

    public function getActionLabel() {
        if ($this->actionLable) {
            return $this->actionLable;
        } elseif ($this->getAction() && $this->getAction()->getId()) {
            return $this->getAction()->getId();
        } else {
            return '--';
        }
    }

    public function setUnionTitle($actionName = NULL) {
        $title['module'] = $this->getModuleLabel();
        $title['controller'] = $this->getControllerLabel();
        if ($actionName !== NULL) {
            $title['action'] = $actionName;
        } elseif ($this->getAction() !== null && $this->getAction()->getId()) {
            $title['action'] = $this->getAction()->getId();
        }

        $this->controllerLable = $title['controller'];
        $this->actionLable = $title['action'];
        ksort($title);
        $this->setPageTitle(implode(' - ', (array) $title));
    }

    public function rendererTile() {
        if ($this->getPageTitle()) {
            return $this->getPageTitle();
        } else {
            return Yii::app()->name;
        }
    }

    public function setBreadcrumbs($array) {
        
    }

    public function getBreadcrumbs() {
        return $this->breadcrumbs = array(
            $this->getControllerLabel() => array('admin'),
            $this->getActionLabel(),
        );
    }

    public function isExceptAction() {
        return preg_match('/privilege\/login|privilege\/logout|privilege\/captcha/i', strtolower($this->getRoute()));
    }

    protected function getAllowActions() {
        /* @var $session AdminhtmlSession */
        $acl = Yii::app()->admin->getState('allowActions');
        if ($acl) {
            $roleAcl = $acl;
        } else {
            $roleId = AdminhtmlAdmin::model()->loadRoleIdByAccount(Yii::app()->admin->name);
            $roleAcl = AdminhtmlRole::model()->getOneAclArray($roleId);
            Yii::app()->admin->setState('allowActions', $roleAcl);
        }
        //print_r($roleAcl);die;

        if (isset($roleAcl[$this->getActionsKey()])) {
            return $roleAcl[$this->getActionsKey()];
        } else {
            return array();
        }
    }

    protected function beforeAction($action) {
        /* @var CWebApplication */
        if ((!$this->isExceptAction() && Yii::app()->admin->getIsGuest() ) || (!$this->isExceptAction() && Yii::app()->admin->getState('area') !== Yii::app()->params['area']['adminhtml'] )
        ) {
            $this->redirect(array('privilege/login', 'referrer' => base64_encode($this->getRoute())));
            return;
        }
        if ($this->getId() === 'default' || Yii::app()->admin->getState('allowActions') === Yii::app()->params['fullAccess'] || in_array(strtolower($this->getAction()->getId()), (array) $this->getAllowActions())
        ) {
            return true;
        } else {
            $this->redirect(array('privilege/noPrivilege', 'returnUrl' => base64_encode(Yii::app()->getRequest()->getUrlReferrer())));
        }
    }

    /*
      //not use
      public function accessRules()
      {
      $access = array( array('deny', 'users'=>array('*'), ) );
      $userAcl= $this->getAllowActions();

      array_unshift($access, array('allow',
      'actions'=> $userAcl,
      'users'=> array(Yii::app()->admin->name )
      )  ) ;
      return $access;
      }
      public function filters()
      {
      return array(
      'accessControl', // perform access control for CRUD operations
      );
      } */

    public function attachLogBehavior($model) {
        if ($model instanceof CActiveRecord) {
            $model->attachBehavior('ARB', 'application.modules.adminh.behaviors.AdminhtmlARB');
        }
    }

}